package com.ruoyi.tj.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.tj.BizError;
import com.ruoyi.tj.entity.SqShequjibenxinxi;
import com.ruoyi.tj.entity.SqShequtijianxinxi;
import com.ruoyi.tj.service.SqShequtijianxinxiService;
import com.ruoyi.tj.vo.BizQueryParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 社区-社区体检信息 前端控制器
 * </p>
 *
 * @author aoliao
 * @since 2024-03-14
 */
@Slf4j
@RestController
@RequestMapping("/api/sq-shequtijianxinxi")
public class SqShequtijianxinxiController {

    @Resource
    private SqShequtijianxinxiService service;

    @PostMapping("/save")
    @ResponseBody
    public AjaxResult saveRecord(@RequestBody SqShequtijianxinxi record) {
        try {
            record = service.saveRecord(record);
            return AjaxResult.success((Object) record.getId());
        } catch (Throwable e) {
            log.error("saveRecord 异常", e);
            return AjaxResult.error(e.getMessage());
        }
    }
    @PostMapping("/saveBatch")
    @ResponseBody
    public AjaxResult saveRecordBath(@RequestBody List<SqShequtijianxinxi> records) {
        try {
            service.saveRecordBath(records);
            List<String> ids = records.stream().map(SqShequtijianxinxi::getId).collect(Collectors.toList());
            return AjaxResult.success(ids);
        } catch (Throwable e) {
            log.error("saveRecord 异常", e);
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/get")
    @ResponseBody
    public AjaxResult getRecord(@RequestBody SqShequtijianxinxi record, @RequestBody(required = false) BizQueryParam params) {
        SqShequtijianxinxi record1 = service.getRecord(record, params);
        return record1 != null ? AjaxResult.success(record1) : AjaxResult.success((Object) "");
    }

    @PostMapping("/get/{id}")
    @ResponseBody
    public AjaxResult getRecord(@PathVariable("id") String id) {
        SqShequtijianxinxi record1 = service.getRecordById(id);
        return record1 != null ? AjaxResult.success(record1) : AjaxResult.success((Object) "");
    }

    @PostMapping("/listPage")
    @ResponseBody
    public AjaxResult listPage(@RequestBody SqShequtijianxinxi record, @RequestBody(required = false) BizQueryParam params) {
        IPage<SqShequtijianxinxi> iPage = service.listPage(record, params);
        return AjaxResult.success(iPage);
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public AjaxResult deleteById(@PathVariable("id") String id) {
        service.deleteRecordById(id);
        return AjaxResult.success();
    }


    @PostMapping("/delete")
    @ResponseBody
    public AjaxResult deleteByIds(@RequestBody String[] ids) {
        //String[] split = ids.split(",");
        service.deleteRecordByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }
}
